﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.HomeLoan;
using CE.Global;

public partial class home_loan_rate_loan_tenure : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int _startYear = 5;
            int _endYear = 35;
            for (int i = _startYear; i <= _endYear; i+=5)
            {
                ddlLoanTenure.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            ddlLoanTenure.Items.Insert(0, new ListItem("Select", "-1"));


            ddlLoanTenure.Attributes.Add("onblur", "ShowValidationEffect('" + ddlLoanTenure.ClientID + "','" + rfvLoanTenure.ClientID + "');");
            ddlRate.Attributes.Add("onblur", "ShowValidationEffect('" + ddlRate.ClientID + "','" + rfvRate.ClientID + "');");
            if (Session[General.HLId] != null)
            {
                //ShowHomeLoan();
            }
            else
            {
                Response.Redirect("home-loan.aspx");
            }
            General.ClearAllSession(this.Page, "HL");
            Session[General.ViewedDetailsId] = LogUserViewedHistory().ToString();
        }

        if (rdoYesOffsetSchemes.Checked || rdoNoOffsetSchemes.Checked) txtOffsetSchemes.Text = "1";
        if (rdoYesLockin.Checked || rdoNoLockin.Checked) txtLockin.Text = "1";
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ChangeValidationEffectServerSide("homeloan");
        if (Page.IsValid)
        {
            Int64 _result = 0;
            HomeLoan _homeloan = new HomeLoan();
            HomeLoanDB _homeloandb = new HomeLoanDB();
            _homeloan.HLId = Convert.ToInt64(Session[General.HLId].ToString());
            _homeloan.StepId = 2;
            _homeloan.HLLoanTenure = Convert.ToInt16(ddlLoanTenure.Text);
            _homeloan.HLInterestRate = Convert.ToInt16(ddlRate.Text);
            _homeloan.HLOffsetSchemes = rdoYesOffsetSchemes.Checked;
            _homeloan.HLLockIn = rdoYesLockin.Checked;
            if (Session[General.UserId] != null)
            {
                _homeloan.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
            }
            else
            {
                _homeloan.RefCEUserId = 0;
            }
            if (Session[General.UserId] != null)
            {
                _homeloan.ViewedDetailId = Convert.ToInt64(Session[General.ViewedDetailsId].ToString());
            }
            else
            {
                _homeloan.ViewedDetailId = 0;
            }
            _homeloan.Status = true;
            _result = _homeloandb.Insert(_homeloan);
            if (_result > 0)
            {
                Response.Redirect("personal-details.aspx");
            }
        }
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        //Loan Tenure
        if (rfvLoanTenure.Enabled && rfvLoanTenure.ValidationGroup == strValidationGroup)
        {
            rfvLoanTenure.Validate();
            if (rfvLoanTenure.IsValid == false)
            {
                ddlLoanTenure.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlLoanTenure.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlLoanTenure.ControlStyle.CssClass = "";
        }

        //Rate
        if (rfvRate.Enabled && rfvRate.ValidationGroup == strValidationGroup)
        {
            rfvRate.Validate();
            if (rfvRate.IsValid == false)
            {
                ddlRate.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlRate.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlRate.ControlStyle.CssClass = "";
        }
    }

    private Int64 LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        return _userviewdb.LogViewedHistory("HL", Request, "Home loan rate and loan tenure details.");
    }
    
}
